
package com.trial.area.invitation.service;

import java.math.BigDecimal;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trial.area.coupon.CouponUtils;
import com.trial.area.coupon.dao.CouponDao;
import com.trial.area.coupon.entity.CouponVO;
import com.trial.area.user.dao.UserInfoDao;
import com.trial.area.user.entity.UserInfoVO;

/**
 * 处理邀请信息
 *
 * @author 朱国军
 * @since 1.0
 * @version 2016年7月31日 朱国军
 */
@Service
public class InvitationService {

    private Logger logger = LoggerFactory.getLogger(InvitationService.class);

    @Autowired
    private UserInfoDao userInfoDao;
    
    @Autowired
    private CouponDao couponDao;

    /**
     * 记录邀请人和被邀请人的绑定关系，并发送优惠券
     *
     * @param invitionCode
     * @param userId
     */
    public void bindingUser(String invitionCode, String userId) {
        UserInfoVO registerUser = userInfoDao.findById(userId, null, null);
        UserInfoVO invitationUser = userInfoDao.findById(null, null, invitionCode);

        if (registerUser == null) {
            logger.error("绑定邀请人和被邀请人时，被邀请人不存在：invitionCode=" + invitionCode + ",userId=" + userId);
            return;
        }
        
        if (invitationUser == null) {
            logger.error("绑定邀请人和被邀请人时，邀请人不存在：invitionCode=" + invitionCode + ",userId=" + userId);
            return;
        }
        
        if (StringUtils.equals(registerUser.getUserId(), invitationUser.getUserId())) {
            logger.error("绑定邀请人和被邀请人时，被邀请人和邀请人是同一人：invitionCode=" + invitionCode + ",userId=" + userId);
            return;
        }

        if (StringUtils.isNotBlank(registerUser.getInvitationUser())) {
            logger.error("绑定邀请人和被邀请人时，被邀请人已有邀请人：invitionCode=" + invitionCode + ",userId=" + userId);
            return;
        }
        
        registerUser.setInvitationUser(invitationUser.getUserId());

        userInfoDao.update(registerUser);
        
        // 邀请活动-注册送券
        CouponVO registerCouponVO = new CouponVO();
        registerCouponVO.setUserId(registerUser.getUserId());
        registerCouponVO.setAcquiringWay("邀请送券-被邀请人");
        registerCouponVO.setApplicationScope(1);
        registerCouponVO.setCouponId(UUID.randomUUID().toString());
        registerCouponVO.setCouponName("邀请送券");
        registerCouponVO.setCouponNo(CouponUtils.createCouponNo("ZC"));
        registerCouponVO.setCouponState(2);
        registerCouponVO.setDataStatus(0);
        registerCouponVO.setDiscountAmount(new BigDecimal(100));
        registerCouponVO.setDiscountType(1);
        registerCouponVO.setExpirationTime(DateTime.now().plusMonths(12).toDate());
        couponDao.insert(registerCouponVO);
        // 邀请活动-邀请人成功邀请送券
        CouponVO invitionCouponVO = new CouponVO();
        invitionCouponVO.setUserId(invitationUser.getUserId());
        invitionCouponVO.setAcquiringWay("邀请送券-邀请人");
        invitionCouponVO.setApplicationScope(1);
        invitionCouponVO.setCouponId(UUID.randomUUID().toString());
        invitionCouponVO.setCouponName("邀请送券");
        invitionCouponVO.setCouponNo(CouponUtils.createCouponNo("YQ"));
        invitionCouponVO.setCouponState(2);
        invitionCouponVO.setDataStatus(0);
        invitionCouponVO.setDiscountAmount(new BigDecimal(100));
        invitionCouponVO.setDiscountType(1);
        invitionCouponVO.setExpirationTime(DateTime.now().plusMonths(12).toDate());
        couponDao.insert(invitionCouponVO);
    }
}
